package com.ecommerce.user.controller;

import com.ecommerce.common.dto.Result;
import com.ecommerce.user.dto.UserLoginRequest;
import com.ecommerce.user.dto.UserLoginResponse;
import com.ecommerce.user.dto.UserRegisterRequest;
import com.ecommerce.user.dto.UserResponse;
import com.ecommerce.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户注册、登录、信息查询等接口")
public class UserController {

    private final UserService userService;

    /**
     * 用户注册
     * @param registerRequest 注册请求
     * @return 用户信息
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "新用户注册接口")
    public Result<UserResponse> register(@RequestBody UserRegisterRequest registerRequest) {
        log.info("收到用户注册请求: {}", registerRequest.getUsername());
        UserResponse userResponse = userService.register(registerRequest);
        return Result.success("注册成功", userResponse);
    }

    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @return 登录响应
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口，返回JWT令牌")
    public Result<UserLoginResponse> login(@RequestBody UserLoginRequest loginRequest) {
        log.info("收到用户登录请求: {}", loginRequest.getUsername());
        UserLoginResponse loginResponse = userService.login(loginRequest);
        return Result.success("登录成功", loginResponse);
    }

    /**
     * 根据ID获取用户信息
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    public Result<UserResponse> getUserById(@PathVariable Long userId) {
        log.info("获取用户信息请求，用户ID: {}", userId);
        UserResponse userResponse = userService.getUserById(userId);
        return Result.success(userResponse);
    }
}